﻿namespace ImageProcessing.UI
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// IMG_Fourier form class
    /// </summary>
    public partial class IMG_Fourier : Form
    {
        /// <summary>
        /// original image bitmap
        /// </summary>
        private Entities.IMG_Image imgOriginal;

        /// <summary>
        /// Edited image bitmap
        /// </summary>
        private Entities.IMG_Image imgEdited;

        /// <summary>
        /// parent Form referance
        /// </summary>
        private UI.IMG_HomeScreen parentForm;

        /// <summary>
        /// fourier class
        /// </summary>
        private ImageProcessor.IMG_FourierClass fourier;

        /// <summary>
        /// Initializes a new instance of the <see cref="IMG_Fourier"/> class.
        /// </summary>
        /// <param name="img_original">The img_original.</param>
        /// <param name="parent">The parent.</param>
        public IMG_Fourier(Entities.IMG_Image img_original, UI.IMG_HomeScreen parent)
        {
            try
            {
                this.InitializeComponent();
                this.parentForm = parent;
                if (img_original != null)
                {
                    this.imgOriginal = img_original;
                    this.picBoxOrig.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(img_original);
                }
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error Loading Form");
            }
        }

        /// <summary>
        /// Handles the Click event of the btnOpen control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnOpen_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp; *.ppm)|*.jpg; *.jpeg; *.gif; *.bmp; *.ppm";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string img_OpenedFilePath = openFileDialog1.FileName;
                    this.imgOriginal = IMG_Processor.IMG_OpenSave.IMG_Open(img_OpenedFilePath);
                    this.picBoxOrig.Image = this.imgOriginal.BitmapOfImage();
                    this.imgEdited = null;
                    this.picBoxEdit.Image = null;
                }
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error, Can't Open Image");
            }
        }

        /// <summary>
        /// Handles the FormClosing event of the IMG_Fourier control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.FormClosingEventArgs"/> instance containing the event data.</param>
        private void IMG_Fourier_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                this.parentForm.ResetImage(this.imgEdited == null ? this.imgOriginal : this.imgEdited);
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error, closing");
            }
        }

        /// <summary>
        /// Handles the Click event of the btn_ToFourier control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Btn_ToFourier_Click(object sender, EventArgs e)
        {
            if (this.imgOriginal == null)
            {
                MessageBox.Show("Please open an image");
                return;
            }

            try
            {
                this.fourier = new ImageProcessor.IMG_FourierClass(this.imgOriginal);
                this.fourier.IMG_CalculateFourier();
                this.redImgPicBox.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.fourier.MagnitudeRed);
                this.greenImgPicBox.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.fourier.MagnitudeGreen);
                this.blueImgPicBox.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.fourier.MagnitudeBlue);
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error Converting to fourier");
            }
        }

        /// <summary>
        /// Handles the Click event of the Restore_btn control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Restore_btn_Click(object sender, EventArgs e)
        {
            if (this.imgOriginal == null)
            {
                MessageBox.Show("Please open an image");
                return;
            }

            if (this.fourier == null)
            {
                MessageBox.Show("Please convert to fourier first");
                return;
            }

            try
            {
                this.imgEdited = this.fourier.RestoreImage();
                Bitmap b;
                b = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgEdited);
                this.picBoxEdit.Image = b;
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error restoring from fourier");
            }
        }

        /// <summary>
        /// Handles the Click event of the btn_IdealHighPass control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Btn_IdealHighPass_Click(object sender, EventArgs e)
        {
            if (this.imgOriginal == null)
            {
                MessageBox.Show("Please open an image");
                return;
            }

            if (this.fourier == null)
            {
                MessageBox.Show("Please convert to fourier first");
                return;
            }

            double d0;
            try
            {
                d0 = double.Parse(this.txtD0.Text);
                if (d0 <= 0)
                {
                    MessageBox.Show("Please enter a value for D0 > 0 ");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("Please enter a value for D0");
                return;
            }

            try
            {
                this.imgEdited = this.fourier.ApplyIdealHighPass(d0);
                Bitmap b;
                b = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgEdited);
                this.picBoxEdit.Image = b;
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error restoring from fourier");
            }
        }

        /// <summary>
        /// Handles the Click event of the Ideal_low_Pass_btn control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Ideal_low_Pass_btn_Click(object sender, EventArgs e)
        {
            if (this.imgOriginal == null)
            {
                MessageBox.Show("Please open an image");
                return;
            }

            if (this.fourier == null)
            {
                MessageBox.Show("Please convert to fourier first");
                return;
            }

            double d0;
            try
            {
                d0 = double.Parse(this.txtD0.Text);
                if (d0 <= 0)
                {
                    MessageBox.Show("Please enter a value for D0 > 0 ");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("Please enter a value for D0");
                return;
            }

            try
            {
                this.imgEdited = this.fourier.ApplyIdealLowPass(d0);
                Bitmap b;
                b = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgEdited);
                this.picBoxEdit.Image = b;
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error restoring from fourier");
            }
        }

        /// <summary>
        /// Handles the Click event of the ButterWorth_LP_btn control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ButterWorth_LP_btn_Click(object sender, EventArgs e)
        {
            if (this.imgOriginal == null)
            {
                MessageBox.Show("Please open an image");
                return;
            }

            if (this.fourier == null)
            {
                MessageBox.Show("Please convert to fourier first");
                return;
            }

            double d0;
            double n;
            try
            {
                d0 = double.Parse(this.txtD0.Text);
                n = double.Parse(this.txtN.Text);
                if (d0 <= 0 || n <= 0)
                {
                    MessageBox.Show("Please enter a value for D0 > 0 , N >=0");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("Please enter a value for D0 & N");
                return;
            }

            try
            {
                this.imgEdited = this.fourier.ApplyButterWorthLowPass(d0, n);
                Bitmap b;
                b = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgEdited);
                this.picBoxEdit.Image = b;
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error restoring from fourier");
            }
        }

        /// <summary>
        /// Handles the Click event of the btn_ButterWorthHighPass control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Btn_ButterWorthHighPass_Click(object sender, EventArgs e)
        {
            if (this.imgOriginal == null)
            {
                MessageBox.Show("Please open an image");
                return;
            }

            if (this.fourier == null)
            {
                MessageBox.Show("Please convert to fourier first");
                return;
            }

            double d0;
            double n;
            try
            {
                d0 = double.Parse(this.txtD0.Text);
                n = double.Parse(this.txtN.Text);
                if (d0 <= 0 || n <= 0)
                {
                    MessageBox.Show("Please enter a value for D0 > 0 , N >=0");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("Please enter a value for D0 & N");
                return;
            }

            try
            {
                this.imgEdited = this.fourier.ApplyButterWorthHighPass(d0, n);
                Bitmap b;
                b = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgEdited);
                this.picBoxEdit.Image = b;
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error restoring from fourier");
            }
        }

        /// <summary>
        /// Handles the Click event of the btn_GaussianHighPass control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Btn_GaussianHighPass_Click(object sender, EventArgs e)
        {
            if (this.imgOriginal == null)
            {
                MessageBox.Show("Please open an image");
                return;
            }

            if (this.fourier == null)
            {
                MessageBox.Show("Please convert to fourier first");
                return;
            }

            double d0;
            try
            {
                d0 = double.Parse(this.txtD0.Text);
                if (d0 <= 0)
                {
                    MessageBox.Show("Please enter a value for D0 > 0");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("Please enter a value for D0");
                return;
            }

            try
            {
                this.imgEdited = this.fourier.ApplyGaussianHighPass(d0);
                Bitmap b;
                b = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgEdited);
                this.picBoxEdit.Image = b;
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error restoring from fourier");
            }
        }

        /// <summary>
        /// Handles the Click event of the Gaussian_LP_btn control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Gaussian_LP_btn_Click(object sender, EventArgs e)
        {
            if (this.imgOriginal == null)
            {
                MessageBox.Show("Please open an image");
                return;
            }

            if (this.fourier == null)
            {
                MessageBox.Show("Please convert to fourier first");
                return;
            }

            double d0;
            try
            {
                d0 = double.Parse(this.txtD0.Text);
                if (d0 <= 0)
                {
                    MessageBox.Show("Please enter a value for D0 > 0");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("Please enter a value for D0");
                return;
            }

            try
            {
                this.imgEdited = this.fourier.ApplyGaussianLowPass(d0);
                Bitmap b;
                b = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgEdited);
                this.picBoxEdit.Image = b;
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error restoring from fourier");
            }
        }
    }
}
